package pCloud.config;

import java.net.InetSocketAddress;
import java.util.ArrayList;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import pCloud.message.RequestedResource;

/**
 * Parsing the configuration file containing client request for data. The file
 * includes: number of resources And a list of resources, each containing: name
 * role dataserver address databaseID table ID column ID (more than 1) joining
 * column ID
 * 
 * @author anhdinh
 * 
 */
public class PCloudRequestConfig {
	static Configuration config;
	public static final String NAME = "name";
	public static final String ROLE = "role";
	public static final String DATAID = "dataId";
	public static final String SERVER = "server";
	public static final String DATABASE = "database";
	public static final String TABLE = "table";
	public static final String COLUMN = "columns";
	public static final String ACTION = "action";
	public static final String JOINING_COLUMN = "joining_column";
	public static final String NRESOURCES = "nResources";
	public static final String CONSTRAINT = "constraint";
	public static final String INPUTDATA = "input_value";

	public static void init(String configFile) throws ConfigurationException {
		config = new PropertiesConfiguration(configFile);
	}

	public static ArrayList<RequestedResource> parseConfig() {
		ArrayList<RequestedResource> resources = new ArrayList<RequestedResource>();

		int n = new Integer(getString(NRESOURCES)).intValue();
		String name = null, role = null, database = null, table = null, joiningColumn = null, constraint = null, action = null;
		String[] serverAddress;
		String[] columns = null;
		String dataId;
		String[] inputValues = null;
		
		for (int i = 0; i < n; i++) {
			if (config.containsKey(NAME+"_"+i))
				name = getString(NAME + "_" + i);
			
			role = getString(ROLE + "_" + i);
			// serverAddress = getString(SERVER + "_" + i).split(" ");
			// database = getString(DATABASE + "_" + i);
			dataId = getString(DATAID + "_" + i);

			table = getString(TABLE + "_" + i);

			if (config.containsKey(COLUMN + "_" + i))
				columns = getString(COLUMN + "_" + i).split(" ");

			action = getString(ACTION + "_" + i);

			if (config.containsKey(JOINING_COLUMN + "_" + i))
				joiningColumn = getString(JOINING_COLUMN + "_" + i);

			if (config.containsKey(CONSTRAINT + "_" + i))
				constraint = getString(CONSTRAINT + "_" + i);
			
			if (config.containsKey(INPUTDATA+"_"+i))
				inputValues = getString(INPUTDATA+"_"+i).split(" ");
			
			RequestedResource rr = new RequestedResource(name, role, dataId,
					table, constraint, action, inputValues);

			if (columns != null)
				for (int j = 0; j < columns.length; j++) {
					rr.addColumnId(columns[j]);
				}

			if (joiningColumn != null)
				rr.addJoiningColumn(joiningColumn);
			resources.add(rr);
		}
		return resources;
	}

	public static String getString(String field) {
		return config.getString(field);
	}
}
